#include "VBottomButtons.h"
#include "VPicture.h"
#include "VButton.h"
#include "VAnimationComponent.h"
#include "VMarquee.h"
#include "VMain.h"
#include "VIndex.h"
#include "VMyZone.h"
#include "VVodWidget.h"

DEFINE_THIS(VBottomButtons);

VBottomButtons::VBottomButtons(void)
{
    SET_THIS();
    setVisible(true);

    _lightbar = new VPicture;
    _toplightbar = new VPicture;
    _toplightbar_mask = new VPicture;
    _spinlogo = new VAnimateComponent;
    _marquee = new VMarquee;
    _index = new VIndex;
    _myzone = new VMyZone;
    _voded_mask = new VButton;
    _voded_widget = new VVodWidget;

    setupLightbar();
    setupTopLightbar();
    setupBottomButtons();
    setupSpinLogo();
    setupMarquee();
    setupIndex();
    setupMyZone();
    setupVodWidget();

    connect();

    insert(_spinlogo);
    insert(_lightbar);
    insert(_toplightbar);
    insert(_toplightbar_mask);
    insert(_marquee);
    insert(_index);
    insert(_myzone);
    insert(_voded_mask);
    insert(_voded_widget);
}

VBottomButtons::~VBottomButtons(void)
{

}

void VBottomButtons::setupBottomButtons()
{
    for (int i = 0; i != leftbtncount; ++i)
    {
        _leftbuttons[i] = new VButton;

        _leftbuttons[i]->setZOrder(3);
        _leftbuttons[i]->loadTexture(VMain::getSkin("bottom_buttons\\l%d.png", i + 1),
                                     VMain::getSkin("bottom_buttons\\l%dp.png", i + 1));
        _leftbuttons[i]->setGeometry(
            110 + i * (commonwidth + xmargin),
            630,
            commonwidth,
            commonheight);

        insert(_leftbuttons[i]);
    }

    for (int i = 0; i != rightbtncount; ++i)
    {
        _rightbuttons[i] = new VButton;

        _rightbuttons[i]->setZOrder(3);
        _rightbuttons[i]->loadTexture(VMain::getSkin("bottom_buttons\\r%d.png", i + 1),
                                      VMain::getSkin("bottom_buttons\\r%dp.png", i + 1));
        _rightbuttons[i]->setGeometry(
            700 + i * (commonwidth + xmargin),
            630,
            commonwidth,
            commonheight);


        insert(_rightbuttons[i]);
    }

    _rightbuttons[5]->setPrivateData(new int(0x54c3));

    _leftbuttons[2]->setNoPopup();
    _leftbuttons[3]->setNoPopup();
    _leftbuttons[5]->setNoPopup();
    _rightbuttons[3]->setNoPopup();
    _rightbuttons[4]->setNoPopup();
    _rightbuttons[5]->setNoPopup();
}

void VBottomButtons::setupLightbar()
{
    _lightbar->loadTexture(VMain::getSkin("lightbar.png"));
    _lightbar->setGeometry(0, VIEWPORT_HEIGHT - 128, 1280, 128);
    _lightbar->setZOrder(1);
}


void VBottomButtons::connect()
{
#ifdef UNDER_CE
    VPlayer* player = VMain::getPlayer();

    // CD,è
    _leftbuttons[1]->setClickHandler(
        CLICK_EVENT_CAST(&VPlayer::cbNext));
    _leftbuttons[3]->setClickHandler(
        CLICK_EVENT_CAST(&VPlayer::cbPlayOrPause));
    _leftbuttons[5]->setClickHandler(
        CLICK_EVENT_CAST(&VPlayer::cbSilentOrNormal));

#endif  // UNDER_CE

    _rightbuttons[1]->setClickHandler(
        CLICK_EVENT_CAST(&VMain::exit));

    _rightbuttons[3]->setClickHandler(
        CLICK_EVENT_CAST(&VMain::handler));

    _rightbuttons[5]->setClickHandler(
        CLICK_EVENT_CAST(&VMain::returnMainUi));

    _spinlogo->setClickHandler(
        CLICK_EVENT_CAST(&VBottomButtons::switchVisibility));

    _voded_mask->setClickHandler(
        CLICK_EVENT_CAST(&VBottomButtons::switchVodWidgetVisibility));
}

bool VBottomButtons::switchVisibility(int, int, void* pd)
{
    getThis()->_index->setVisible(!getThis()->_index->isVisible());

    return true;
}


void VBottomButtons::setupSpinLogo()
{
    int i;

    _spinlogo->setZOrder(2);
    _spinlogo->setGeometry(575, 560, 128, 128);
    //_spinlogo->addFlags(VAnimateComponent::INVERT_DIRECTION);

    _spinlogo->setFPS(8);

    for (i = 1; i != 31; ++i)
    {
        _spinlogo->addFrame(
            VMain::getSkin("logo\\%03d.png", i)
        );
    }

    _spinlogo->startAnimate();
}

void VBottomButtons::setupTopLightbar()
{
    _toplightbar->setGeometry(0, 0, 1280, 64);
    _toplightbar_mask->setGeometry(0, 0, 1280, 64);

    _toplightbar->setZOrder(1);
    _toplightbar->loadTexture(
        VMain::getSkin("toplightbar.png")
    );

    _toplightbar_mask->setZOrder(4);
    _toplightbar_mask->loadTexture(
        VMain::getSkin("toplightbar_mask.png")
    );
}

void VBottomButtons::setupMarquee()
{
    _marquee->setPixelsPerSecond(100);
    _marquee->setText("五道杠 杀了拉登!!!! 本报头条!!!! 话说:......详情请登录CCAV官网.全是AV");
    _marquee->setMargin(400);
    _marquee->setZOrder(3);
    _marquee->setGeometry(149, 5, 973, 64);

    _marquee->startScroll();
}

void VBottomButtons::setupIndex()
{
    _index->setZOrder(0);
}

void VBottomButtons::setupMyZone()
{
}

void VBottomButtons::setupVodWidget()
{
    _voded_mask->setGeometry(VIEWPORT_WIDTH - 40,
                             VIEWPORT_HEIGHT - 40,
                             40,
                             40);
    
    _voded_widget->setGeometry(AUTOSET, AUTOSET, AUTOSET, AUTOSET);
    _voded_widget->setZOrder(0);
}


bool VBottomButtons::switchVodWidgetVisibility(int, int, void* pd)
{
    getThis()->_voded_widget->setVisible(!getThis()->_voded_widget->isVisible());

    return true;
}

